#!/usr/bin/env bash
# install_keyboard_shortcut=<Control><Shift>V

# Source the script 'common-functions.sh'.
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
ROOT_DIR=$(grep --only-matching "^.*scripts[^/]*" <<<"$SCRIPT_DIR")
source "$ROOT_DIR/common-functions.sh"

_main() {
    local output_dir=""

    # Execute initial checks.
    case "${XDG_SESSION_TYPE,,}" in
    "x11") _check_dependencies "command=xclip" ;;
    "wayland") _check_dependencies "command=wl-paste; package=wl-clipboard" ;;
    *)
        _display_error_box "Unknown session type!"
        _exit_script
        ;;
    esac

    _display_wait_box "2"
    output_dir=$(_get_working_directory_alternative)

    # Execute the function '_main_task' for each file in parallel.
    _run_task_parallel "$(_get_urls_from_clipboard)" "$output_dir"
    _display_result_box "$output_dir"
}

_main_task() {
    local input_file=$1
    local output_dir=$2
    local std_output=""

    cd "$output_dir" || return 1

    if ! [[ -e "$input_file" ]]; then
        return 1
    fi

    # Run the main process.
    output_file=$(_get_output_filename "$input_file" "$output_dir" "par_extension_opt=preserve")
    std_output=$(ln -s -- "$input_file" "$output_file" 2>&1)
    _check_output "$?" "$std_output" "$input_file" "$output_file" || return 1
}

_get_urls_from_clipboard() {
    local urls=""

    if [[ "${XDG_SESSION_TYPE,,}" == "x11" ]]; then
        urls=$(xclip -quiet -selection clipboard -o)
    elif [[ "${XDG_SESSION_TYPE,,}" == "wayland" ]]; then
        urls=$(wl-paste)
    fi
    urls=$(grep --only-matching --perl-regexp "^(file://|('\")?/)(.*)" <<<"$urls")
    urls=$(sed "s|^['\"]||g; s|['\"]$||g" <<<"$urls")
    urls=$(sort -u <<<"$urls")

    if [[ -z "$urls" ]]; then
        _display_error_box "There are no valid paths in the clipboard!"
        _exit_script
    fi

    urls=$(_convert_text_to_filenames "$urls")
    urls=$(_text_uri_decode "$urls")
    printf "%s" "$urls"
}

_get_working_directory_alternative() {
    local working_directory=""

    # Try to use the information provided by the file manager.
    if [[ -v "CAJA_SCRIPT_CURRENT_URI" ]]; then
        working_directory=$CAJA_SCRIPT_CURRENT_URI
    elif [[ -v "NEMO_SCRIPT_CURRENT_URI" ]]; then
        working_directory=$NEMO_SCRIPT_CURRENT_URI
    elif [[ -v "NAUTILUS_SCRIPT_CURRENT_URI" ]]; then
        working_directory=$NAUTILUS_SCRIPT_CURRENT_URI
    fi

    if [[ -n "$working_directory" ]] && [[ "$working_directory" == "file://"* ]]; then
        working_directory=$(_text_uri_decode "$working_directory")
    else
        # Files selected in the search screen (or orther possible cases).
        working_directory=""
    fi

    if [[ -z "$working_directory" ]]; then
        # NOTE: The working directory can be detected by using the directory name
        # of the first input file. Some file managers do not send the working
        # directory for the scripts, so it is not precise to use the 'pwd' command.
        local item_1=""
        local item_2=""
        item_1=$(cut -d "$FIELD_SEPARATOR" -f 1 <<<"$INPUT_FILES")
        item_2=$(cut -d "$FIELD_SEPARATOR" -f 2 <<<"$INPUT_FILES")

        if [[ -n "$item_1" ]]; then
            if [[ -n "$item_2" ]] && [[ "$item_1" != "$item_2" ]]; then
                working_directory=$(_get_filename_dir "$item_1")
            elif [[ -f "$item_1" ]]; then
                working_directory=$(_get_filename_dir "$item_1")
            elif [[ -d "$item_1" ]]; then
                working_directory=$(_get_filename_full_path "$item_1")
            fi
        else
            working_directory=$(pwd)
        fi
    fi

    printf "%s" "$working_directory"
}

_main "$@"
